#include "DS1302.h"
#include "intrins.h"

void ds1302_write_byte(unsigned char addr, unsigned char byte) 
{
	char i = 0;
	CLK = 0;
	CE = 1;
	_nop_();_nop_();
	
	for(;i<8;++i)
	{
		if(addr & 0x01) IO = 1; else IO = 0;
		_nop_();
		CLK = 1;
		_nop_();
		CLK = 0;
		addr >>= 1;
	}
	_nop_();
	
	for(i=0;i<8;++i) 
	{
		if(byte & 0x01) IO = 1; else IO = 0;
		_nop_();
		CLK = 1;
		_nop_();
		CLK = 0;
		byte >>= 1;
	}
	_nop_();
	CE = 0;
	_nop_();
	
}


unsigned char ds1302_read_byte(unsigned char addr)
{
	char i = 0;
	unsigned char res = 0;

	CLK = 0;
	CE = 1;
	_nop_();_nop_();
	
	for(;i<8;++i)
	{
		if(addr & 0x01) IO = 1; else IO = 0;
		_nop_();
		CLK = 1;
		_nop_();
		CLK = 0;
		addr >>= 1;
	}
	_nop_();
	
	for(i=0;i<8;++i)
	{
		res >>= 1;
		if(IO) res |= 0x80;
		CLK = 1;
		_nop_();
		CLK = 0;
		_nop_();
	}
	
	CE = 0;
	_nop_();
	
	
CLK = 1;
_nop_();
IO = 0;
_nop_();
IO = 1;
_nop_();
	return res;
}
